package cn.zd.erp.auth.resource.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Controller;





import cn.zd.erp.auth.resource.service.ebi.ResourceEbi;
import cn.zd.erp.auth.resource.vo.Tresource;
import cn.zd.erp.shiro.SessionInfo;
import cn.zd.erp.util.base.BaseAction;
import cn.zd.erp.util.base.Grid;
import cn.zd.erp.util.base.Json;
import cn.zd.erp.util.base.Resource;
import cn.zd.erp.util.base.Tree;

@Controller
@Scope(value = "prototype")
public class ResourceAction extends BaseAction implements SessionAware{
	private Map session;
	private Json json = new Json();
	private Grid grid;

	@Autowired
	private ResourceEbi resourceEbi;
	private List<Resource> list;
	private List<Tree> tree;
	private Tresource tresource;
	private boolean flag;

	public List<Resource> getList() {
		return list;
	}

	public List<Tree> getTree() {
		return tree;
	}

	public Json getJson() {
		return json;
	}

	public Grid getGrid() {
		return grid;
	}

	// 返回全部资源树
	public String dataGrid() {
		list = resourceEbi.datatree();
		return "listjson";
	}

	// 返回当前用户的菜单
	public String menutree() {
		SessionInfo sessionInfo =  (SessionInfo) session.get("sessionInfo");
		System.out.println(sessionInfo.getName()+"session");
		tree = resourceEbi.getTree(sessionInfo);
		return "tree";
	}

	// 所有菜单flag是true 只是菜单 false是包括所有资源
	public String allTree() {
		tree = resourceEbi.listAllTree(flag);
		return "tree";
	}

	// 新增用户
	public String sava() {
		/**
		 * 可能增加一个主菜单会报错
		 */
		// 先查询有没有同名有的话添加失败
		Tresource resource = resourceEbi.getByName(tresource.getName());
		if (resource != null) {
			json.setMsg("资源已存在!");
		}

		// 没有就开始添加
		else {
			try {
				resourceEbi.save(tresource);
				json.setSuccess(true);
				json.setMsg("添加成功！");
			} 
			catch(InvalidDataAccessApiUsageException e)
			{
				json.setMsg("添加主菜单成功！");
			}
			catch (Exception e) {
				System.out.println(e.toString());
				json.setMsg(e.getMessage());
			}
		}

		return "resultjson";
	}

	// 页面跳转
	// 编辑用户
	public String page() {
		if (tresource != null) {
			tresource = resourceEbi.get(tresource.getId());
		}
		return SUCCESS;
	}

	// 修改保存
	public String edit() {
		try {
			resourceEbi.save(tresource);
			json.setSuccess(true);
			json.setMsg("修改成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}

		return "resultjson";
	}

	public String delete() {
		try {
			resourceEbi.delete(tresource);
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return "resultjson";
	}

	public Tresource getTresource() {
		return tresource;
	}

	public void setTresource(Tresource tresource) {
		this.tresource = tresource;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
